package org.linlinjava.litemall.admin.controller;

import javax.validation.constraints.NotNull;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.beans.dto.MerchandiseAllinone;
import org.linlinjava.litemall.admin.beans.vo.MerchandiseVo;
import org.linlinjava.litemall.admin.service.AdminMerchandiseService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallMerchandise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品管理控制器
 * 货品库存
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/merchandise")
@Validated
public class AdminMerchandiseController {

    /**
     * 管理商品服务
     */
    @Autowired
    private AdminMerchandiseService adminMerchandiseService;


    @GetMapping("/revise")
    @LogAnno
    public Object revise() {
        return adminMerchandiseService.revise();
    }

    /**
     * 列表
     * 查询总部货品列表
     *
     * @param name          名字
     * @param merchandiseSn 商品sn
     * @param page          页面
     * @param limit         限制
     * @param sort          排序
     * @param order         订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String name, String merchandiseSn,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return adminMerchandiseService.list(name, merchandiseSn, page, limit, sort, order);
    }

    /**
     * 创建
     * 添加货品
     *
     * @param litemallMerchandise litemall商品
     * @return {@link Object}
     */
    @PostMapping("/create")
    @LogAnno
    public Object create(@RequestBody LitemallMerchandise litemallMerchandise) {
        return adminMerchandiseService.create(litemallMerchandise);
    }

    /**
     * 更新
     * 修改货品信息
     *
     * @param merchandiseAllinone 商品allinone
     * @param shopId              商店id
     * @return {@link Object}
     */
    @PutMapping("/update")
    @LogAnno
    public Object update(@RequestBody MerchandiseAllinone merchandiseAllinone, @LoginAdminShopId Integer shopId) {
        return adminMerchandiseService.update(merchandiseAllinone, shopId);
    }


    /**
     * 读
     * 读取货品信息
     *
     * @param id     id
     * @param shopId 商店id
     * @return {@link Object}
     */
    @GetMapping("/read")
    @LogAnno
    public Object read(Integer id, @LoginAdminShopId Integer shopId) {
        return adminMerchandiseService.read(id, shopId);
    }

    /**
     * 商品记录列表
     * 出库入库列表
     *
     * @param merchandiseId   商品id
     * @param merchandiseName 商品名称
     * @param orderSn         订单sn
     * @param shopId          商店id
     * @param page            页面
     * @param limit           限制
     * @param sort            排序
     * @param order           订单
     * @return {@link Object}
     */
    @GetMapping("/merchandiseRecordList")
    @LogAnno
    public Object merchandiseRecordList(Integer merchandiseId, String merchandiseName, String orderSn, @LoginAdminShopId Integer shopId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return adminMerchandiseService.goodsProductRecordList(merchandiseId, merchandiseName, orderSn, shopId, page, limit, sort, order);
    }

    /**
     * 所有
     * 查询所有库存
     *
     * @return {@link Object}
     */
    @GetMapping("/all")
    @LogAnno
    public Object all(@RequestParam(name = "goodsId",required = false)Integer goodsId) {
        return adminMerchandiseService.all(goodsId);
    }


    /**
     * 添加数量
     * 补充库存
     *
     * @param vo 签证官
     * @return {@link Object}
     */
    @PutMapping("/addNumber")
    @LogAnno
    public Object addNumber(@RequestBody MerchandiseVo vo) {
        return adminMerchandiseService.addNumber(vo);
    }


    /**
     * 数
     * 门店查询库存数量
     *
     * @param shopId        商店id
     * @param merchandiseId 商品id
     * @return {@link Object}
     */
    @GetMapping("/count")
    @LogAnno
    public Object count(@LoginAdminShopId Integer shopId, @NotNull  @RequestParam(value = "merchandiseId") Integer merchandiseId) {
        return adminMerchandiseService.count(shopId, merchandiseId);
    }

    /**
     * 更新价格
     * 修改销售价
     *
     * @param vo     签证官
     * @param shopId 商店id
     * @return {@link Object}
     */
    @PutMapping("/updatePrice")
    @LogAnno
    public Object updatePrice(@RequestBody MerchandiseVo vo, @LoginAdminShopId Integer shopId) {
        if(shopId == null){
            return ResponseUtil.badArgument();
        }
        return adminMerchandiseService.updatePrice(vo, shopId);
    }

    /**
     * 统计数据
     *
     * @param merchandiseSn   商品sn
     * @param merchandiseName 商品名称
     * @param beginTime       开始时间
     * @param endTime         结束时间
     * @param page            页面
     * @param limit           限制
     * @return {@link Object}
     */
    @LogAnno
    @GetMapping("/statistics")
    public Object statistics(String merchandiseSn, String merchandiseName,
    		@RequestParam("startTime") String beginTime,
    		@RequestParam String endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit){
    	return adminMerchandiseService.statistics(merchandiseName, merchandiseSn, beginTime, endTime, page, limit);
    }

}
